package com.beordie.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.beordie.mapper.TeamMapper;
import com.beordie.model.Team;
import com.beordie.model.TeamExample;
import com.beordie.bean.TeamBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeamService {
    @Resource
    private TeamMapper teamMapper;

    /**
     * @description 多条件分页查询
     * @author 30500
     * @date 2021/7/23 18:22
     * @type [java.lang.Integer 开始页码, java.lang.Integer 显示条数, com.beordie.bean.TeamBean 条件封装对象]
     * @return com.github.pagehelper.PageInfo<com.beordie.model.Team> 分页结果
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Team> queryByPage(Integer pageNum, Integer pageSize, TeamBean teamBean){
        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Team> teams = teamMapper.selectByExample(teamBean);
        if (teams == null || teams.size() == 0)
            return null;
        return new PageInfo<>(teams);
    }

    /**
     * @description 根据主键删除信息（逻辑删除）
     * @author 30500
     * @date 2021/7/24 7:27
     * @type [int 删除的球队的ID]
     * @return int 返回删除影响的条数
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteById(int teamId) {
        Team team = new Team();
        team.setTeamId(teamId);
        team.setIsDel(1);

        // 逻辑删除，将标志更新
        int delete = teamMapper.updateByPrimaryKeySelective(team);
        return delete;
    }

    /**
     * @description 插入一条数据
     * @author 30500
     * @date 2021/7/24 7:28
     * @type [com.beordie.model.Team 插入球队的基本信息对象]
     * @return int 返回插入影响的条数
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertTeam(Team team) {
        int insert = teamMapper.insert(team);
        return insert;
    }

    /**
     * @description 根据主键进行更新
     * @author 30500
     * @date 2021/7/24 7:41
     * @type [com.beordie.model.Team 需要更新的球队对象]
     * @return int 返回跟新影响的条数
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateById(Team team) {
        int update = teamMapper.updateByPrimaryKeySelective(team);
        return update;
    }

    /**
     * @description 根据主键进行信息查询
     * @author 30500
     * @date 2021/7/24 8:13
     * @type [int 主键ID]
     * @return com.beordie.model.Team 返回查询的结果对象
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Team queryById(int teamId) {
        Team team = teamMapper.selectByPrimaryKey(teamId);
        return team;
    }
}
